package org.jeecg.modules.work.repair.controller;

import java.util.Arrays;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.utils.RandomCodeUtils;
import org.jeecg.modules.work.dispatch.entity.BikeWorkDispatch;
import org.jeecg.modules.work.repair.entity.BikeWorkRepair;
import org.jeecg.modules.work.repair.service.IBikeWorkRepairService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.work.sweep.entity.BikeWorkSweep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 维修工单
 * @Author: 月月鸟
 * @Date:   2021-05-31
 * @Version: V1.0
 */
@Api(tags="维修工单")
@RestController
@RequestMapping("/repair/bikeWorkRepair")
@Slf4j
public class BikeWorkRepairController extends JeecgController<BikeWorkRepair, IBikeWorkRepairService> {
	@Autowired
	private IBikeWorkRepairService bikeWorkRepairService;

	/**
	 * 分页列表查询
	 *
	 * @param bikeWorkRepair
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="维修工单-分页列表查询", notes="维修工单-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BikeWorkRepair bikeWorkRepair,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BikeWorkRepair> queryWrapper = QueryGenerator.initQueryWrapper(bikeWorkRepair, req.getParameterMap());
		Page<BikeWorkRepair> page = new Page<BikeWorkRepair>(pageNo, pageSize);
		IPage<BikeWorkRepair> pageList = bikeWorkRepairService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 维修工单拼接
	  *
	  * @param bikeWorkRepair
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @ApiOperation(value="维修工单拼接-分页列表查询", notes="维修工单拼接-分页列表查询")
	 @GetMapping(value = "/pageList")
	 public Result<?> pageList(BikeWorkRepair bikeWorkRepair,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 return bikeWorkRepairService.pageList(pageNo,pageSize,bikeWorkRepair);
	 }

	 /**
	  * 维修工单拼接 查看
	  *
	  * @param bikeWorkRepair
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "维修工单拼接-查看")
	 @ApiOperation(value="维修工单拼接-查看", notes="维修工单拼接-查看")
	 @GetMapping(value = "/pageListById")
	 public Result<?> pageListById(@RequestParam String id,BikeWorkRepair bikeWorkRepair) {
		 return bikeWorkRepairService.pageListById(id);
	 }
	/**
	 *   添加
	 *
	 * @param bikeWorkRepair
	 * @return
	 */
	@AutoLog(value = "维修工单-添加")
	@ApiOperation(value="维修工单-添加", notes="维修工单-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BikeWorkRepair bikeWorkRepair) {
		bikeWorkRepair.setCode(RandomCodeUtils.getRandomCode(BikeConstant.work_repair_open));
		if (StringUtils.isEmpty(bikeWorkRepair.getStationCode())){
			return Result.error("网点编号为空");
		}
		if (StringUtils.isEmpty(bikeWorkRepair.getDeviceCode())){
			return Result.error("设备编号为空");
		}
		String YN = bikeWorkRepairService.checkAddData(bikeWorkRepair.getStationCode(),bikeWorkRepair.getDeviceCode());
		if (YN.equals(BikeConstant.y_n_yes)){
			if (StringUtils.isNotEmpty(bikeWorkRepair.getIsDispatch())){
				if(bikeWorkRepair.getIsDispatch().equals(BikeConstant.y_n_yes)){
					bikeWorkRepair.setDispatchTime(new Date());
				}
			}
			bikeWorkRepairService.save(bikeWorkRepair);
			return Result.OK("添加成功！");
		}else {
			return Result.error(YN);
		}

	}

	/**
	 *  编辑
	 *
	 * @param bikeWorkRepair
	 * @return
	 */
	@AutoLog(value = "维修工单-编辑",operateType = 3)
	@ApiOperation(value="维修工单-编辑", notes="维修工单-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BikeWorkRepair bikeWorkRepair) {
		bikeWorkRepairService.updateById(bikeWorkRepair);
		return Result.OK("编辑成功!");
	}
	 /**
	  *
	  *
	  * @param bikeWorkRepair
	  * @return
	  */
	 @AutoLog(value = "维修-再次派单")
	 @ApiOperation(value="维修-再次派单", notes="维修-再次派单")
	 @PostMapping(value = "/addAgain")
	 public Result<?> addAgain(@RequestBody BikeWorkRepair bikeWorkRepair) {
	 	try {
			//如果原工单为新增 再次派单来源为 原工单id（用于相关历史工单）
			if (bikeWorkRepair.getWorkSource().equals("1")){
				bikeWorkRepair.setSourceId(bikeWorkRepair.getCode());
			}
			//-----------需要修改
			//是否解决
			bikeWorkRepair.setIsSolve(BikeConstant.y_n_no);
			//已派单
			bikeWorkRepair.setIsDispatch(BikeConstant.y_n_yes);
			//新转工单时间
			bikeWorkRepair.setDispatchTime(new Date());
			//新工单code
			bikeWorkRepair.setCode(RandomCodeUtils.getRandomCode(BikeConstant.work_dispatch_open));
			//来源为再次派单
			bikeWorkRepair.setWorkSource("4");
			//--------------需要删除
			bikeWorkRepair.setContent(null);
			bikeWorkRepair.setActualTime(null);
			bikeWorkRepair.setActualAddress(null);
			bikeWorkRepair.setCreateBy(null);
			bikeWorkRepair.setCreateTime(null);
			bikeWorkRepair.setUpdateBy(null);
			bikeWorkRepair.setUpdateTime(null);
			bikeWorkRepairService.save(bikeWorkRepair);
		}catch (Exception e){
			return Result.error("派单失败！");
		}
		 return Result.OK("派单成功！");
	 }
	 /**
	  *  根据id 派单
	  * @param webData
	  * @return
	  */
	 @AutoLog(value = "维修工单-派单",operateType = 3)
	 @ApiOperation(value="维修工单-派单", notes="维修工单-派单")
	 @PutMapping(value = "/dispatch")
	 public Result<?> dispatch(@RequestBody BikeWorkRepair webData) {
		 BikeWorkRepair bikeWorkRepair = new BikeWorkRepair();
		 bikeWorkRepair.setId(webData.getId());
		 String yn = bikeWorkRepairService.judgeDispatch(bikeWorkRepair);
		 if (yn.equals(BikeConstant.y_n_yes)){
			 bikeWorkRepair.setIsDispatch(BikeConstant.y_n_yes);
			 bikeWorkRepair.setDispatchTime(new Date());
			 bikeWorkRepairService.updateById(bikeWorkRepair);
			 return Result.OK("派单成功!");
		 }else {
			 return Result.error("不可重复派单");
		 }
	 }
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "维修工单-通过id删除")
	@ApiOperation(value="维修工单-通过id删除", notes="维修工单-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {

		return bikeWorkRepairService.deleteById(id);
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "维修工单-批量删除")
	@ApiOperation(value="维修工单-批量删除", notes="维修工单-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		String[] split = ids.split(",");
		//判断派单情况
		String yn = bikeWorkRepairService.judgeDelet(split);
		if (yn.equals(BikeConstant.y_n_yes)){
			this.bikeWorkRepairService.removeByIds(Arrays.asList(ids.split(",")));
			return Result.OK("批量删除成功!");
		}else {
			return Result.error("无法删除已派单工单");
		}
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "维修工单-通过id查询")
	@ApiOperation(value="维修工单-通过id查询", notes="维修工单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BikeWorkRepair bikeWorkRepair = bikeWorkRepairService.getById(id);
		if(bikeWorkRepair==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(bikeWorkRepair);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param bikeWorkRepair
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BikeWorkRepair bikeWorkRepair) {
        //return super.exportXls(request, bikeWorkRepair, BikeWorkRepair.class, "维修工单");
        return bikeWorkRepairService.exportXls(request,bikeWorkRepair);
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BikeWorkRepair.class);
    }

}
